using UnityEngine;
using System.Collections;

public class SphereCollisionScript : MonoBehaviour {
	
	protected Rect windowRect = new Rect(20, 20, 120, 50);
	bool win = false;
	
	int winboxWidth = 200;
	int winboxHeight = 50;
	
	bool firstCollide = true;
	
	string sphere1 = "Sphere1";
	string sphere2 = "Sphere2";
	string cube1 = "Cube1";
	string cube2 = "Cube2";
	
	Vector3 startupPositionSphere1;
	Quaternion startupRotationSphere1;
	Vector3 startupPositionSphere2;
	Quaternion startupRotationSphere2;
	Vector3 startupPositionCube1;
	Quaternion startupRotationCube1;
	Vector3 startupPositionCube2;
	Quaternion startupRotationCube2;
	
	
	void OnGUI () 
	{
		if (win)
		{
			windowRect = new Rect(
				Screen.width / 2 - winboxWidth / 2, 
				Screen.height / 2 - winboxHeight / 2, 
				winboxWidth, 
				winboxHeight);
			GUILayout.Window(0, windowRect, handleWin, "Oh yeah!");
			
		}
	}
	
	void resetGame()
	{
		GameObject.Find(sphere1).transform.position = startupPositionSphere1;
		GameObject.Find(sphere1).transform.rotation = startupRotationSphere1;
		GameObject.Find(sphere2).transform.position = startupPositionSphere2;
		GameObject.Find(sphere2).transform.rotation = startupRotationSphere2;
		
		GameObject.Find(cube1).transform.position = startupPositionCube1;
		GameObject.Find(cube1).transform.rotation = startupRotationCube1;
		GameObject.Find(cube2).transform.position = startupPositionCube2;
		GameObject.Find(cube2).transform.rotation = startupRotationCube2;
	}
	
	void handleWin(int bla)
	{
		resetGame();
		if (this.gameObject.name == "Sphere1")
		{
			if (GUILayout.Button("You win player 1!",GUILayout.MinHeight(50)))
			{	
				win = false;
			}
		}
		else
		{
			if (GUILayout.Button("You win player 2!",GUILayout.MinHeight(50)))
			{
				win = false;
			}	
		}
	}
	
	void handleStartupPositions()
	{
		startupPositionSphere1 = GameObject.Find(sphere1).transform.position;
		startupRotationSphere1 = GameObject.Find(sphere1).transform.rotation;
		startupPositionSphere2 = GameObject.Find(sphere2).transform.position;
		startupRotationSphere2 = GameObject.Find(sphere2).transform.rotation;
		startupPositionCube1 = GameObject.Find(cube1).transform.position;
		startupRotationCube1 = GameObject.Find(cube1).transform.rotation;
		startupPositionCube2 = GameObject.Find(cube2).transform.position;
		startupRotationCube2 = GameObject.Find(cube2).transform.rotation;
	}
	
	void OnCollisionEnter(Collision collision) {
		
//		Debug.Log(this.gameObject.name);
		
		// safe startup position
		if (firstCollide)
		{
			handleStartupPositions();
			firstCollide = false;
		}
		
		// handle a death area collision
		if (collision.gameObject.name.Contains("DeathArea"))
		{
			if (this.gameObject.name == "Sphere1")
			{
				this.gameObject.transform.position = startupPositionSphere1;
				this.gameObject.transform.rotation = startupRotationSphere1;
			}
			else
			{
				this.gameObject.transform.position = startupPositionSphere2;
				this.gameObject.transform.rotation = startupRotationSphere2;
			}
		}
		
		// handle the portals
		if (collision.gameObject.name.Contains("PortalStart"))
		{
			GameObject portalEnd = GameObject.Find("PortalEnd" + 
				collision.gameObject.name.Substring(
				collision.gameObject.name.Length - 1));
			
			this.gameObject.transform.position = portalEnd.transform.position;
		}
		
		// handle the winning situation
		if (collision.gameObject.name == "TargetSphere")
		{
			Debug.Log("WIIIIIIIN");
			win = true;
		}
		
	}
}
